package tv.youxing.android.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import com.kymjs.rxvolley.client.HttpParams;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import de.hdodenhof.circleimageview.CircleImageView;
import tv.youxing.android.R;
import tv.youxing.android.custom.BaseViewHolder;
import tv.youxing.android.custom.CustomToast;
import tv.youxing.android.custom.GridViewForScrollView;
import tv.youxing.android.entity.DianZhanModel;
import tv.youxing.android.entity.DynamicDetailsPingLunModel;
import tv.youxing.android.entity.FriendModel;
import tv.youxing.android.entity.ImageArrayModel;
import tv.youxing.android.ui.DynamicDetailsActivity;
import tv.youxing.android.ui.DynamicDetailsActivity_new;
import tv.youxing.android.ui.LoginActivity;
import tv.youxing.android.utils.AppConst;
import tv.youxing.android.utils.GsonUtils;
import tv.youxing.android.utils.PicassoUtils;
import tv.youxing.android.utils.ShareUtils;
import tv.youxing.android.utils.StaticClass;

/**
 * Created by apple on 2017/4/20.
 */

/**
 * 项目名：   youxing
 * 包名：     tv.youxing.android .adapter
 * 文件名：   DynamicAdapter
 * 创建者：   叶克武
 * 创建时间： 2017/4/20 上午11:02
 * 描述：    TODO
 */
public class DynamicAdapter extends BaseAdapter {

    private Context context;
    private List<FriendModel.ResultBean.DataBean> dynamicModelList = new ArrayList<>();
    private LayoutInflater layoutInflater;
    private ListView listView;
    private Activity mActivity;
    List<FriendModel.ResultBean.DataBean.ImageArrayBean> imgUrl = new ArrayList<>();

    public DynamicAdapter(Activity activity ,Context context, List<FriendModel.ResultBean.DataBean> dynamicModelList, ListView listView) {
        this.mActivity = activity;
        this.dynamicModelList = dynamicModelList;
        this.context = context;
        this.layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        this.listView = listView;
    }

    @Override
    public int getCount() {
        return dynamicModelList==null || dynamicModelList.size()==0 ? 0 : dynamicModelList.size();
    }

    @Override
    public Object getItem(int position) {
        return dynamicModelList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View vi = convertView;




        if (convertView == null) {
            vi = layoutInflater.inflate(R.layout.layout_item_dynamic,null);

            CircleImageView ivAvatar = BaseViewHolder.get(vi,R.id.ivIcon);
            TextView tvName = BaseViewHolder.get(vi,R.id.tvName);
            TextView tvContent = BaseViewHolder.get(vi,R.id.tvContent);
            TextView tvTime = BaseViewHolder.get(vi,R.id.tvTime);
            final ImageView ivPraise = BaseViewHolder.get(vi,R.id.ivPraise);
            final TextView tvPraise = BaseViewHolder.get(vi,R.id.tvPraise);
            TextView tvComment = BaseViewHolder.get(vi,R.id.tvComment);
            RelativeLayout llPinglun = BaseViewHolder.get(vi,R.id.pinglun);
            GridViewForScrollView gridView = BaseViewHolder.get(vi, R.id.picGridView);


            //头像
            PicassoUtils.loadImageViewHolder(context,dynamicModelList.get(position).getPublish_user().getAvatar(),R.drawable.logo,R.drawable.logo,ivAvatar);
            ivAvatar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent  = new Intent(context, DynamicDetailsActivity_new.class);
                    intent.putExtra("user_id",dynamicModelList.get(position).getPublish_user().getUser_id()+"");
                    context.startActivity(intent);
//                    L.d("user_id--2:" + dynamicModelList.get(position).getPublish_user().getUser_id());
                }
            });



            //名称
            tvName.setText(dynamicModelList.get(position).getPublish_user().getUsername());
            //发布内容
            tvContent.setText(dynamicModelList.get(position).getContent());

            //时间
            tvTime.setText(dynamicModelList.get(position).getPublish_time().toString());


            final int[] like = {dynamicModelList.get(position).getIs_like()};
            //点赞

            if (like[0] == 1) {
                ivPraise.setSelected(true);
            }else{
                //点赞的点击事件
                ivPraise.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //post请求简洁版实现
                        HttpParams params = new HttpParams();
                        params.putHeaders("Authorization","Bearer "+ ShareUtils.getString(context, AppConst.TOKEN,""));
                        params.put("resource_id",dynamicModelList.get(position).getTweet_id());
                        params.put("resource_type",2);


                        RxVolley.post(StaticClass.DianZan, params, new HttpCallback() {
                            @Override
                            public void onSuccess(String t) {
                                DianZhanModel dianZhanModel = GsonUtils.GsonToBean(t,DianZhanModel.class);

                                if (dianZhanModel.getCode() == AppConst.TOKENERROR) {
                                        mActivity.startActivity(new Intent(mActivity,LoginActivity.class));
                                        return;
                                }

                                CustomToast.INSTANCE.showToast(dianZhanModel.getStatus());
                                if (dianZhanModel.getCode() == 200) {
                                    if (dianZhanModel.getResult().getIs_like() == 1) {
                                        ivPraise.setSelected(true);
                                        int count = Integer.parseInt(dynamicModelList.get(position).getLike_count())+1;
                                        tvPraise.setText(count+"");
                                        like[0] = 1;
                                    } else {
                                        ivPraise.setSelected(false);
                                        int count = Integer.parseInt(dynamicModelList.get(position).getLike_count());
                                        tvPraise.setText(count+"");
                                        like[0] = 0;
                                    }
                                }else{
                                    ivPraise.setSelected(false);
                                    like[0] = 0;
                                }
                            }
                        });
                    }
                });
            }

            //点赞数
            tvPraise.setText(dynamicModelList.get(position).getLike_count());

            //说说评论数
            tvComment.setText(dynamicModelList.get(position).getComment_count());

            //评论界面
            llPinglun.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, DynamicDetailsActivity.class);

                    Bundle bundle = new Bundle();


                    DynamicDetailsPingLunModel model = new DynamicDetailsPingLunModel();
                    DynamicDetailsPingLunModel.PublishUserBean model_user = new DynamicDetailsPingLunModel.PublishUserBean();

                    model.setContent(dynamicModelList.get(position).getContent());
                    model.setPublish_time(dynamicModelList.get(position).getPublish_time());
                    model.setIs_like(like[0]);
                    model.setTweet_id(dynamicModelList.get(position).getTweet_id());
                    model.setLike_count(dynamicModelList.get(position).getLike_count());

                    model_user.setAvatar(dynamicModelList.get(position).getPublish_user().getAvatar());
                    model_user.setUsername(dynamicModelList.get(position).getPublish_user().getUsername());


                    model.setPublish_user(model_user);


                    bundle.putSerializable("data",model);


                    intent.putExtras(bundle);
                    context.startActivity(intent);
                }
            });

            imgUrl = dynamicModelList.get(position).getImage_array();

            final List<ImageArrayModel> imgs = new ArrayList<>();

            for (int i = 0; i < dynamicModelList.get(position).getImage_array().size(); i++) {

                ImageArrayModel model = new ImageArrayModel();
                model.setImage_url(dynamicModelList.get(position).getImage_array().get(i).getImage_url());
                model.setThumbnail(dynamicModelList.get(position).getImage_array().get(i).getThumbnail());
                imgs.add(model);
            }

            gridView.setAdapter(new ImageAdapter(mActivity,context,imgs));
        }


/*

        ViewHolder viewHolder = null;
        if (viewHolder == null){
            viewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.layout_item_dynamic,null);
            viewHolder.root = (LinearLayout) convertView.findViewById(R.id.root);
            viewHolder.ivAvatar = (CircleImageView) convertView.findViewById(R.id.ivIcon);
            viewHolder.tvName = (TextView) convertView.findViewById(R.id.tvName);
            viewHolder.tvContent = (TextView) convertView.findViewById(R.id.tvContent);
            viewHolder.gridView = (CustomGridView) convertView.findViewById(R.id.picGridView);
            viewHolder.tvTime = (TextView) convertView.findViewById(R.id.tvTime);
            viewHolder.ivPraise = (ImageView) convertView.findViewById(R.id.ivPraise);
            viewHolder.tvPraise = (TextView) convertView.findViewById(R.id.tvPraise);
            viewHolder.tvComment = (TextView) convertView.findViewById(R.id.tvComment);
            viewHolder.llPinglun = (RelativeLayout) convertView.findViewById(R.id.pinglun);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }

        List<String> urlList = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
//            urlList.add(getBmpUrl());
        }


//        viewHolder.root.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Intent  intent  = new Intent(context, DynamicDetailsActivity_new.class);
//                intent.putExtra("resource_id",dynamicModelList.get(position).getTweet_id()+"");
//                context.startActivity(intent);
//            }
//
//        });


        urlList = dynamicModelList.get(position).getImages();
        //用户头像
        PicassoUtils.loadImageViewHolder(context,dynamicModelList.get(position).getPublish_user().getAvatar(),R.drawable.logo,R.drawable.logo,viewHolder.ivAvatar);
        viewHolder.ivAvatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent  intent  = new Intent(context, DynamicDetailsActivity_new.class);
                intent.putExtra("user_id",dynamicModelList.get(position).getPublish_user().getUser_id()+"");
                context.startActivity(intent);
            }
        });



        //名称
        viewHolder.tvName.setText(dynamicModelList.get(position).getPublish_user().getUsername());
        //发布内容
        viewHolder.tvContent.setText(dynamicModelList.get(position).getContent());


//        urlList = dynamicModelList.get(position).getImages();
        //发布的图片
        viewHolder.gridView.setAdapter(new ImageAdapter(mActivity,context, urlList));
        viewHolder.gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                L.i("viewHolder.gridView:  "+position);
            }
        });

        //时间
        viewHolder.tvTime.setText(dynamicModelList.get(position).getPublish_time().toString());

        //点赞
        if (dynamicModelList.get(position).getIs_like() == 1) {
            viewHolder.ivPraise.setSelected(true);
        }else{
            //点赞的点击事件
            final ViewHolder finalViewHolder = viewHolder;

            viewHolder.ivPraise.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //post请求简洁版实现
                    HttpParams params = new HttpParams();
                    params.putHeaders("Authorization","Bearer "+ ShareUtils.getString(context, AppConst.TOKEN,""));
                    params.put("resource_id",dynamicModelList.get(position).getTweet_id());
                    params.put("resource_type",2);


                    RxVolley.post(StaticClass.DianZan, params, new HttpCallback() {
                        @Override
                        public void onSuccess(String t) {
                            DianZhanModel dianZhanModel = GsonUtils.GsonToBean(t,DianZhanModel.class);

                            if (dianZhanModel.getCode() == 200) {
                                finalViewHolder.ivPraise.setSelected(true);
                                int count = Integer.parseInt(dynamicModelList.get(position).getLike_count())+1;
                                finalViewHolder.tvPraise.setText(count+"");
                            }else{
                                finalViewHolder.ivPraise.setSelected(false);
                            }
                        }
                    });
                }
            });
        }






        //点赞数
        viewHolder.tvPraise.setText(dynamicModelList.get(position).getLike_count());

        //说说评论数
        viewHolder.tvComment.setText(dynamicModelList.get(position).getComment_count());

        //评论界面
        viewHolder.llPinglun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, DynamicDetailsActivity.class);

                Bundle bundle = new Bundle();


                DynamicDetailsPingLunModel model = new DynamicDetailsPingLunModel();
                DynamicDetailsPingLunModel.PublishUserBean model_user = new DynamicDetailsPingLunModel.PublishUserBean();

                model.setContent(dynamicModelList.get(position).getContent());
                model.setPublish_time(dynamicModelList.get(position).getPublish_time());
                model.setIs_like(dynamicModelList.get(position).getIs_like());
                model.setTweet_id(dynamicModelList.get(position).getTweet_id());
                model.setLike_count(dynamicModelList.get(position).getLike_count());

                model_user.setAvatar(dynamicModelList.get(position).getPublish_user().getAvatar());
                model_user.setUsername(dynamicModelList.get(position).getPublish_user().getUsername());


                model.setPublish_user(model_user);


                bundle.putSerializable("data",model);


                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });

        return convertView;



*/


return vi;
    }



     String[] IMAGES = new String[] {
            "http://photo.scol.com.cn/hdp/img/attachement/jpg/site2/20140422/00219b7b064914c05a234b.jpg"};

    /**
     * 随机产生的一个图片Url
     *
     * @return
     */
     String getBmpUrl() {
        int index = new Random().nextInt(IMAGES.length);
        return IMAGES[index];
    }

    class ViewHolder{
        LinearLayout root;
        private CircleImageView ivAvatar;
        private TextView tvName;
        private TextView tvContent;
        private GridViewForScrollView gridView;
        private TextView tvTime;
        private ImageView ivPraise;
        private TextView tvPraise;
        private TextView tvComment;
        private RelativeLayout llPinglun;
    }

}
